<template>
  <div class="comment-post">
    <van-field
      class="post-field"
      v-model="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入留言"
      show-word-limit
    />
    <van-button class="post-btn" @click="onPost">发布</van-button>
  </div>
</template>

<script>
import { addComment } from "@/api/comment";
export default {
  name: "CommentPost",
  components: {},
  props: {
    target: {
      type: [String, Number, Object],
      required: true
    },
    art_id: {
      type: [String, Number, Object],
      default: null
    },
    list: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      message: ""
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onPost() {
      if (!this.message) return this.$toast("请输入内容");
      try {
        let { data: res } = await addComment({
          target: this.target,
          content: this.message,
          art_id: this.art_id
        });
        // console.log(res);
        this.list.unshift(res.data.new_obj);
        this.$emit("close");
        this.$emit("update-count");

        this.$toast.success("评论成功");
      } catch (error) {
        this.$toast.fail("评论失败");
      }
    }
  }
};
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>
